package com.credit.core.util;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URLEncoder;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.alibaba.dubbo.common.utils.StringUtils;

import jodd.datetime.JDateTime;

public class Lang {
	
	public static final int DEFAULT_PRECISION = 2;
	
	public static String formatMoney(BigDecimal b, int len) {
		return b.setScale(len, RoundingMode.HALF_DOWN).toString();
	}
	
	public static String formatMoney(BigDecimal b) {
		return formatMoney(b, DEFAULT_PRECISION);
	}
	
	public static String formatMoney(double b, int len) {
		return formatMoney(new BigDecimal(b), len);
	}
	
	public static String formatMoney(double b) {
		return formatMoney(new BigDecimal(b), DEFAULT_PRECISION);
	}
	public static Integer[] strs2ints(String[] strs) {
		int len = strs.length;
		Integer[] ints = new Integer[len];
		for(int i=0;i<len;i++) {
			ints[i] = Integer.parseInt(strs[i]);
		}
		return ints;
	}
	
	public static String getUUID() {
		return UUID.randomUUID().toString().replaceAll("-", "");
	}
	
	public static boolean isSameDirectory(String path1, String path2) {
		path1 = path1.replaceAll("\\\\", "").replaceAll("\\/", "");
		path2 = path2.replaceAll("\\\\", "").replaceAll("\\/", "");
		return path1.equals(path2);
	}
	
	public static String subFilePath(String path, String prefix) {
		path = path.replaceAll("\\\\", "\\/");
		prefix = prefix.replaceAll("\\\\", "\\/");
		return path.replaceFirst(prefix, "");
	}
	
	public static String getFormatSize(double size) {
		double kiloByte = size/1024;
		if(kiloByte < 1) {
			return size + "Byte(s)";
		}
		
		double megaByte = kiloByte/1024;
		if(megaByte < 1) {
			BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
			return result1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "KB";
		}
		
		double gigaByte = megaByte/1024;
		if(gigaByte < 1) {
			BigDecimal result2  = new BigDecimal(Double.toString(megaByte));
			return result2.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "MB";
		}
		
		double teraBytes = gigaByte/1024;
		if(teraBytes < 1) {
			BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
			return result3.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "GB";
		}
		BigDecimal result4 = new BigDecimal(teraBytes);
		return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "TB";
	}
	
	public static int toInt(Object obj) {
		return obj==null||StringUtils.isBlank(obj+"")?0:Integer.parseInt(obj+"");
	}
	
	public static String getTimeStamp10() {
		String timestamp = System.currentTimeMillis()+"";
		return timestamp.substring(0, 10);
	}
	
	public static String getApiFlowNo() {
		Random r = new Random();
		return "api" + new JDateTime().toString("YYYYMMDDhhmmss") + r.nextInt(10) + r.nextInt(10);
	}
	
	public static String appendparam(String url, String key, String value) {
		if(url.indexOf("?") == -1) {
			url += "?" + key + "=" + value;
		} else {
			url += "&" + key + "=" + value;
		} 
		return url;
	}
	
	public static String encodeURL(String url, String encode) {
		try {
			return URLEncoder.encode(url, encode);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	public static String delHTMLTag(String htmlStr){ 
		if(htmlStr==null) return null;
        String regEx_script="<script[^>]*?>[\\s\\S]*?<\\/script>"; //定义script的正则表达式 
        String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式 
        String regEx_html="<[^>]+>"; //定义HTML标签的正则表达式 
         
        Pattern p_script=Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE); 
        Matcher m_script=p_script.matcher(htmlStr); 
        htmlStr=m_script.replaceAll(""); //过滤script标签 
         
        Pattern p_style=Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE); 
        Matcher m_style=p_style.matcher(htmlStr); 
        htmlStr=m_style.replaceAll(""); //过滤style标签 
         
        Pattern p_html=Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE); 
        Matcher m_html=p_html.matcher(htmlStr); 
        htmlStr=m_html.replaceAll(""); //过滤html标签 

        return htmlStr.trim(); //返回文本字符串 
    } 
	
	public static void main(String[] args) {
		System.out.println(getApiFlowNo());
	}
	
}
